<?php

/**
 * Controller_semester
 * 学期
 */ 

class Controller_semester extends MyApp {
	
	/** 二级联动 学期,学月 **/
	public function actionMonthJson ()
	{	
				
		$semester_id = (int)$this->get->semester_id;
		$week_lists = $this->db->fetchAll("SELECT distinct `semester_month` FROM `tbl_week` WHERE semester_id='{$semester_id}'");
		if(!empty($week_lists)){
			echo json_encode($week_lists);
		}else{
			exit("0");
		}
	}
	/** 二级联动 学期，周次 **/
	public function actionWeekJson ()
	{
		$semester_id = (int)$this->get->semester_id;
		$week_lists = $this->db->fetchAll("SELECT * FROM `tbl_week` WHERE semester_id='{$semester_id}'");
		if(!empty($week_lists)){
			echo json_encode($week_lists);
		}else{
			exit("0");
		}
		
	}
	public function __construct()
	{

		parent::__construct();

		$this->model->semester = YOU::loadModel('semester');

	}

	/** 首页 **/
	public function actionIndex()
	{

		$this->tpl->display();
	}

	/** 学期列表 **/
	public function actionSemestertList ()
	{
		// 分页
		YOU::loadLib('page', "", '0');
		$tmp = $this->db->fetchRow("SELECT COUNT(1) AS count FROM `tbl_semester`");
		$count = $tmp['count'];
		$page_total = ceil($count/PAGE_LIMIT);

		// 当前页
		$p = intval($_GET['p']);
		if ($p == 0){
			$p  = 1;
		}
		if ($p > 1){
			$start = PAGE_LIMIT * ($p-1);
		}else{
			$start = 0;
		}

		$semester_lists = $this->db->fetchAll("SELECT * FROM `tbl_semester` ORDER BY semester_id DESC LIMIT {$start}, ".PAGE_LIMIT."");

		$page = new Page(array('total' => $count, 'perpage' => PAGE_LIMIT));
		$page->next_page   = '下一页>';
		$page->pre_page    = '<上一页';
		$page->style_path  = "static/page_style";

		$this->tpl->assign("page", $page->show(4, "yahoo"));
		$this->tpl->assign("semester_lists", $semester_lists);
		$this->tpl->display('semester/semester.list.tpl.php');

	}
	/** 学期添加 **/
	public function actionSemesteradd()
	{

		if ($_POST){

			$semester_name     	    = $this->post->semester_name;
			$semester_xname         = $this->post->semester_xname;
			$start_time             = $this->post->start_time;
			$end_time    		   	= $this->post->end_time;
			$create_time    		= $this->post->create_time;
			$update_time    		= $this->post->update_time;

			if (empty($semester_name)){
				Helper_Client::alert("请填写学期");
			}
			if (empty($semester_xname)){
				Helper_Client::alert("请填写学年");
			}
			if (empty($start_time)){
				Helper_Client::alert("请填写工作开始时间");
			}
			if (empty($end_time)){
				Helper_Client::alert("请填写工作结束时间");
			}
			if (empty($create_time)){
				Helper_Client::alert("请填写学习开始时间");
			}
			if (empty($update_time)){
				Helper_Client::alert("请填写学习结束时间");
			}

			// 写入数据库
			$info = array(

			"semester_name"      	=> $semester_name,
			"semester_xname"      	=> $semester_xname,
			"start_time"        	=> $start_time,
			"end_time"   		    => $end_time,
			"status"                => 1,
			"create_time"        	=> $create_time,
			"update_time"   		=> $update_time,

			);
			$this->db->do_insert("tbl_semester", $info);
			$semester_id = $this->db->insert_id();

			// 周次
			$days = (strtotime($end_time) - strtotime($start_time)) / 86400;

			// 周数
			$w = 1;

			for ($i=0; $i <= $days; $i++){
				// 日期递增
				$now = date("Y-m-d", (strtotime($start_time) + 86400*$i) );

				$arr[$w][] = $now;

				if (date("w", strtotime($now)) == 0){
					$w += 1;
				}
			}
			
			// 学月
			$semester_month = 0;
			foreach ($arr as $key => $value){
				if (($key-1)%4 == 0){
					$semester_month += 1;
				}
		
				$info = array(
				"semester_id"			=> $semester_id,
				"semester_month"		=> $semester_month,				
				"week_number"			=> $key,
				"start_time"			=> min($value),
				"end_time"				=> max($value),
				"status"                => 1,
				"create_time"           => $create_time,
				"update_time"           => $update_time,
				);

				$this->db->do_insert("tbl_week", $info);
			}

			// 跳转
			Helper_Client::location("添加成功", "index.php?a=semester.semestertList");
		}

		$semester_lists = $this->model->semester->getAll();
		$this->tpl->assign("semester_lists", $semester_lists);
		$this->tpl->display('semester/semester.add.tpl.php');
	}
	
	/** 修改学期 **/
	public function actionsemesterUpdate()
	{

		$semester_id   = (int)$this->get->semester_id;
		$semester_data = $this->model->semester->getinfoy($semester_id);

		if (empty($semester_data)){
			Helper_Client::alert("栏目不存在");
		}

		if ($_POST){

			$semester_name     	    = $this->post->semester_name;
			$semester_xname         = $this->post->semester_xname;
			$start_time             = $this->post->start_time;
			$end_time    		   	= $this->post->end_time;
			$create_time    		= $this->post->create_time;
			$update_time    		= $this->post->update_time;

			// 写入数据库
			$info = array(

			"semester_name"      	=> $semester_name,
			"semester_xname"      	=> $semester_xname,
			"start_time"        	=> $start_time,
			"end_time"   		    => $end_time,
			"status"                => 1,
			"create_time"           => $create_time,
			"update_time"           => $update_time,
			);
			$this->db->do_update("tbl_semester", $info, "semester_id='{$semester_id}'");
			$this->db->query("DELETE  FROM `tbl_week` WHERE semester_id='{$semester_id}'");
			
			// 周次
			$days = (strtotime($end_time) - strtotime($start_time)) / 86400;

			// 周数
			$w = 1;

			for ($i=0; $i <= $days; $i++){
				// 日期递增
				$now = date("Y-m-d", (strtotime($start_time) + 86400*$i) );

				$arr[$w][] = $now;

				if (date("w", strtotime($now)) == 0){
					$w += 1;
				}
			}
			
			// 学月
			$semester_month = 0;
			foreach ($arr as $key => $value){
				if (($key-1)%4 == 0){
					$semester_month += 1;
				}
		
				$info = array(
				"semester_id"			=> $semester_id,
				"semester_month"		=> $semester_month,				
				"week_number"			=> $key,
				"start_time"			=> min($value),
				"end_time"				=> max($value),
				"status"                => 1,
				"create_time"           => $create_time,
				"update_time"           => $update_time,
				);

				$this->db->do_insert("tbl_week", $info);
			}
			
			// 跳转
			Helper_Client::location("修改成功", "index.php?a=semester.semestertList");
		}

		$this->tpl->assign("semester_data", $semester_data);
		
		$this->tpl->display('semester/semester.update.tpl.php');
	}

	/** 删除学期资料 **/
	public function actionsemesterDel()
	{
		$semester_id = $this->get->semester_id;

		$this->model->semester->del($semester_id);

		// 跳转
		Helper_Client::location("删除成功", "index.php?a=semester.semestertlist");
	}


	/** 批量删除 **/
	public function actionDelAll()
	{
		$pids = $this->post->pids;
		if (!empty($pids)){
			foreach ($pids as $key => $value){
				$this->db->query("DELETE FROM `tbl_semester` WHERE semester_id='{$value}'");					
			}
		}
		// 跳转
		Helper_Client::location("删除成功", "index.php?a=semester.semestertlist");
	}	

}

?>